package com.example.myapplication1;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication1.dao.ConcaterDao;
import com.example.myapplication1.model.Concater;
import com.example.myapplication1.model.User;

public class AddActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG =AddActivity.class.getSimpleName();
    private final Activity mContext =AddActivity.this;
    private ConcaterDao concaterDao;

    private Button btn_save;
    private Button btn_back;
    private TextView tv_title;
    private EditText et_name;
    private EditText et_phone;
    private ImageView iv_pic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        initUI();
        initData();
    }

    private void initUI(){
        btn_save =findViewById(R.id.btn_save);
        btn_back =findViewById(R.id.btn_back);
        tv_title =findViewById(R.id.tv_title);
        et_name =findViewById(R.id.et_name);
        et_phone =findViewById(R.id.et_phone);
        iv_pic =findViewById(R.id.iv_pic);
    }

    private void initData(){
        concaterDao =new ConcaterDao(mContext);
    }

    @Override
    public void onClick(View view) {
        int id=view.getId();
        switch (id){
            case R.id.btn_save:
                //1.获取输入框中的内容
                String name=et_name.getText().toString();
                String phone=et_phone.getText().toString();
                //判断内容是否合法
                //account.trim():表示去除左右空格
                if (name.trim().length()==0){
                    Toast.makeText(mContext,"姓名不能为空", Toast.LENGTH_SHORT).show();
                    return;//中断方法
                }
                if (phone.trim().length()==0){
                    Toast.makeText(mContext,"手机号不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }

                //判断手机号是否重复
                if(concaterDao.isExistByPhone(phone)){
                    Toast.makeText(mContext,"手机号已存在，请重新输入",Toast.LENGTH_SHORT).show();
                    return;
                }

                //将数据打包到User对象中
                Concater bean=new Concater(0,name,phone,"");
                //添加到集合中
                concaterDao.insert(bean);
                //返回到登录界面
                finish();
                break;
            case R.id.btn_back:
                break;
        }
    }
}